<?php 
/*php无非就是 查 删 改
我的人生无非就是 想要长大 很快长大 希望回到小时候
*/
require '../core/config.php';
// 辅助函数 生成16位密钥
function generaterandomkey($length = 16) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomKey = '';
    for ($i = 0; $i < $length; $i++) {
        $randomKey .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomKey;
}

// 辅助函数 获取用户 IP 地址
function getip() {
    if (isset($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

// 辅助函数 检测用户是否存在
function accountexists($pdo, $account) {
    $sql = "SELECT COUNT(*) FROM star_user WHERE account = :account";
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(':account', $account, PDO::PARAM_STR);
    $stmt->execute();
    return $stmt->fetchColumn() > 0;
}

function adduser($pdo, $account, $password, $username, $user_introduce = '') {
    try {
        if (accountexists($pdo, $account)) {
            return ['status' => 'error', 'message' => '用户已存在'];
        }
        $hashedPassword = password_hash($password, PASSWORD_DEFAULT);
        $user_key = generaterandomkey();
        $user_ip = getip();
        $mail = $account . '@qq.com';
        $sql = "INSERT INTO star_user (account, password, username, user_introduce, user_key, user_ip, mail) VALUES (:account, :password, :username, :user_introduce, :user_key, :user_ip, :mail)";
        $stmt = $pdo->prepare($sql);
        $stmt->bindParam(':account', $account, PDO::PARAM_STR);
        $stmt->bindParam(':password', $hashedPassword, PDO::PARAM_STR);
        $stmt->bindParam(':username', $username, PDO::PARAM_STR);
        $stmt->bindParam(':user_introduce', $user_introduce, PDO::PARAM_STR);
        $stmt->bindParam(':user_key', $user_key, PDO::PARAM_STR);
        $stmt->bindParam(':user_ip', $user_ip, PDO::PARAM_STR);
        $stmt->bindParam(':mail', $mail, PDO::PARAM_STR);
        if ($stmt->execute()) {
            return ['status' => 'success', 'message' => '用户添加成功', 'user_key' => $user_key];
        } else {
            return ['status' => 'error', 'message' => '用户添加失败'];
        }
    } catch (PDOException $e) {
        return ['status' => 'error', 'message' => '用户添加失败: ' . $e->getMessage()];
    }
}

// 检索所有用户的信息
function getAllUsersData($pdo) {
    try {
        $sql = "SELECT * FROM star_user";
        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    } catch (PDOException $e) {
        die('查询用户数据失败: ' . $e->getMessage());
    }
}

// 更新用户数据
function updateUser($pdo, $uid, $account, $password, $username, $user_introduce, $user_title, $user_avatar, $vip_time, $user_key, $user_balance, $user_ip, $mail, $user_token, $user_docu, $user_permissions, $cloud_disk, $cloud_condition) {
    try {
        if (!empty($password)) {
            $hashedPassword = password_hash($password, PASSWORD_DEFAULT);
        } else {
            $sql = "SELECT password FROM star_user WHERE uid = :uid";
            $stmt = $pdo->prepare($sql);
            $stmt->bindParam(':uid', $uid, PDO::PARAM_INT);
            $stmt->execute();
            $hashedPassword = $stmt->fetchColumn();
        }

        $sql = "UPDATE star_user 
                SET account = :account, password = :password, username = :username, 
                    user_introduce = :user_introduce, user_title = :user_title, 
                    user_avatar = :user_avatar, vip_time = :vip_time, user_key = :user_key, 
                    user_balance = :user_balance, user_ip = :user_ip, mail = :mail, 
                    user_token = :user_token, user_docu = :user_docu, 
                    user_permissions = :user_permissions, cloud_disk = :cloud_disk, 
                    cloud_condition = :cloud_condition
                WHERE uid = :uid";
        $stmt = $pdo->prepare($sql);
        $stmt->bindParam(':uid', $uid, PDO::PARAM_INT);
        $stmt->bindParam(':account', $account, PDO::PARAM_STR);
        $stmt->bindParam(':password', $hashedPassword, PDO::PARAM_STR);
        $stmt->bindParam(':username', $username, PDO::PARAM_STR);
        $stmt->bindParam(':user_introduce', $user_introduce, PDO::PARAM_STR);
        $stmt->bindParam(':user_title', $user_title, PDO::PARAM_STR);
        $stmt->bindParam(':user_avatar', $user_avatar, PDO::PARAM_STR);
        $stmt->bindParam(':vip_time', $vip_time, PDO::PARAM_STR);
        $stmt->bindParam(':user_key', $user_key, PDO::PARAM_STR);
        $stmt->bindParam(':user_balance', $user_balance, PDO::PARAM_STR);
        $stmt->bindParam(':user_ip', $user_ip, PDO::PARAM_STR);
        $stmt->bindParam(':mail', $mail, PDO::PARAM_STR);
        $stmt->bindParam(':user_token', $user_token, PDO::PARAM_STR);
        $stmt->bindParam(':user_docu', $user_docu, PDO::PARAM_STR);
        $stmt->bindParam(':user_permissions', $user_permissions, PDO::PARAM_INT);
        $stmt->bindParam(':cloud_disk', $cloud_disk, PDO::PARAM_STR);
        $stmt->bindParam(':cloud_condition', $cloud_condition, PDO::PARAM_STR);
        return $stmt->execute();
    } catch (PDOException $e) {
        die('更新用户数据失败: ' . $e->getMessage());
    }
}

// 删除用户数据
function deleteUser($pdo, $uid) {
    try {
        $sql = "DELETE FROM star_user WHERE uid = :uid";
        $stmt = $pdo->prepare($sql);
        $stmt->bindParam(':uid', $uid, PDO::PARAM_INT);
        return $stmt->execute();
    } catch (PDOException $e) {
        die('删除用户数据失败: ' . $e->getMessage());
    }
}

// 获取所有帖子分类数据
function getForumClassifyData($pdo) {
    try {
        $sql = "SELECT * FROM star_forum_classify";
        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    } catch (PDOException $e) {
        die('查询帖子分类数据失败: '. $e->getMessage());
    }
}

// 更新帖子分类数据
function updateForumClassifyData($pdo, $uid, $name, $introduce, $head_portrait, $forum_condition, $account) {
    try {
        $sql = "UPDATE star_forum_classify 
                SET name = :name, introduce = :introduce, head_portrait = :head_portrait, 
                    forum_condition = :forum_condition, account = :account, time = NOW()
                WHERE uid = :uid";
        $stmt = $pdo->prepare($sql);
        $stmt->bindParam(':uid', $uid, PDO::PARAM_INT);
        $stmt->bindParam(':name', $name, PDO::PARAM_STR);
        $stmt->bindParam(':introduce', $introduce, PDO::PARAM_STR);
        $stmt->bindParam(':head_portrait', $head_portrait, PDO::PARAM_STR);
        $stmt->bindParam(':forum_condition', $forum_condition, PDO::PARAM_INT);
        $stmt->bindParam(':account', $account, PDO::PARAM_STR);
        return $stmt->execute();
    } catch (PDOException $e) {
        die('更新帖子分类数据失败: '. $e->getMessage());
    }
}

// 删除帖子分类数据
function deleteForumClassifyData($pdo, $uid) {
    try {
        $sql = "DELETE FROM star_forum_classify WHERE uid = :uid";
        $stmt = $pdo->prepare($sql);
        $stmt->bindParam(':uid', $uid, PDO::PARAM_INT);
        return $stmt->execute();
    } catch (PDOException $e) {
        die('删除帖子分类数据失败: '. $e->getMessage());
    }
}


function getForumPosts($pdo) {
    try {
        $sql = "SELECT * FROM star_forum";
        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    } catch (PDOException $e) {
        die('查询帖子数据失败: '. $e->getMessage());
    }
}

function updateForumPost($pdo, $uid, $title, $content, $img, $author, $praise, $visit, $forum_condition) {
    try {
        $sql = "UPDATE star_forum 
                SET title = :title, content = :content, img = :img, 
                    author = :author, praise = :praise, visit = :visit, 
                    forum_condition = :forum_condition, modify_time = NOW()
                WHERE uid = :uid";
        $stmt = $pdo->prepare($sql);
        $stmt->bindParam(':uid', $uid, PDO::PARAM_INT);
        $stmt->bindParam(':title', $title, PDO::PARAM_STR);
        $stmt->bindParam(':content', $content, PDO::PARAM_STR);
        $stmt->bindParam(':img', $img, PDO::PARAM_STR);
        $stmt->bindParam(':author', $author, PDO::PARAM_STR);
        $stmt->bindParam(':praise', $praise, PDO::PARAM_STR);
        $stmt->bindParam(':visit', $visit, PDO::PARAM_STR);
        $stmt->bindParam(':forum_condition', $forum_condition, PDO::PARAM_STR);

        // 执行更新操作并检查结果
        if ($stmt->execute()) {
            return true;
        } else {
            return false;
        }
    } catch (PDOException $e) {
        // 输出详细的错误信息用于调试
        echo '更新帖子数据失败: '. $e->getMessage();
        return false;
    }
}

function deleteForumPost($pdo, $uid) {
    try {
        $sql = "DELETE FROM star_forum WHERE uid = :uid";
        $stmt = $pdo->prepare($sql);
        $stmt->bindParam(':uid', $uid, PDO::PARAM_INT);
        return $stmt->execute();
    } catch (PDOException $e) {
        die('删除帖子数据失败: '. $e->getMessage());
    }
}

//系统信息获取
function get_system_data($pdo) {
    $sql = "SELECT * FROM star_system WHERE uid=1";
    try {
        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        return $result;
    } catch (PDOException $e) {
        json_response(500, '查询失败: ' . $e->getMessage());
    }
}

//帖子部分详情图表
function get_forum_posts($pdo) {
    $sql = "SELECT uid, title, issue_time, modify_time, forum_condition FROM star_forum";
    try {
        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $results;
    } catch (PDOException $e) {
        json_response(500, '查询帖子信息失败: ' . $e->getMessage());
    }
}

//帖子发布图表
function get_daily_post_counts($pdo) {
    $sql = "SELECT DATE(issue_time) AS date, COUNT(*) AS count FROM star_forum GROUP BY DATE(issue_time) ORDER BY date";
    try {
        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

        $data = [];
        foreach ($results as $row) {
            $data[] = [
                'date' => $row['date'],
                'count' => (int) $row['count']
            ];
        }
        return $data;
    } catch (PDOException $e) {
        json_response(500, '查询帖子发布数量失败: ' . $e->getMessage());
    }
}

//用户注册图表
function get_weekly_registrations($pdo) {
    $sql = "SELECT DATE(reg_time) AS date, COUNT(*) AS count FROM star_user WHERE reg_time >= DATE_SUB(NOW(), INTERVAL 1 YEAR) GROUP BY DATE(reg_time)";
    try {
        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

        $data = [];
        foreach ($results as $row) {
            $data[] = [
                'date' => $row['date'],
                'count' => (int) $row['count']
            ];
        }
        return $data;
    } catch (PDOException $e) {
        json_response(500, '查询一周内注册用户数量失败: ' . $e->getMessage());
    }
}

// 用户数量
function get_user_count($pdo) {
    $sql = "SELECT COUNT(*) AS user_count FROM star_user";
    try {
        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        return (int) $result['user_count'];
    } catch (PDOException $e) {
        json_response(500, '查询失败: ' . $e->getMessage());
    }
}

// 通知数量
function get_notification($pdo) {
    $sql = "SELECT COUNT(*) AS notification_count FROM star_notification";
    try {
        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        return (int) $result['notification_count'];
    } catch (PDOException $e) {
        json_response(500, '查询失败: ' . $e->getMessage());
    }
}

// 帖子数量
function get_forum($pdo) {
    $sql = "SELECT COUNT(*) AS forum_count FROM star_forum";
    try {
        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        return (int) $result['forum_count'];
    } catch (PDOException $e) {
        json_response(500, '查询失败: ' . $e->getMessage());
    }
}

// 更新 star_system 表中的字段
function update_field($pdo, $field, $value) {
    $sql = "UPDATE star_system SET $field = :value WHERE uid=1";
    try {
        $stmt = $pdo->prepare($sql);
        $stmt->bindParam(':value', $value);
        $stmt->execute();
        return true;
    } catch (PDOException $e) {
        json_response(500, '更新失败: ' . $e->getMessage());
    }
}

// 密码修改
function update_user_password($pdo, $account, $newPassword) {
    $hashedPassword = password_hash($newPassword, PASSWORD_DEFAULT);
    $sql = "UPDATE star_user SET password = :password WHERE account = :account";
    try {
        $stmt = $pdo->prepare($sql);
        $stmt->bindParam(':password', $hashedPassword);
        $stmt->bindParam(':account', $account);
        $stmt->execute();
        return ['status' => 'success', 'message' => '密码更新成功'];
    } catch (PDOException $e) {
        return ['status' => 'error', 'message' => '密码更新失败: ' . $e->getMessage()];
    }
}

// 获取各个字段的函数
function get_app_name($data) {
    return $data['app_name'] ?? '';
}

function get_app_domain($data) {
    return $data['app_domain'] ?? '';
}

function get_app_introduce($data) {
    return $data['app_introduce'] ?? '';
}

function get_app_initiate($data) {
    return $data['app_initiate'] ?? '';
}

function get_app_time($data) {
    return $data['app_time'] ?? '';
}

function get_app_startup_diagram($data) {
    return $data['app_startup_diagram'] ?? '';
}

function get_app_announcement_title($data) {
    return $data['app_announcement_title'] ?? '';
}

function get_app_announcement_body($data) {
    return $data['app_announcement_body'] ?? '';
}

function get_app_advertisement_title($data) {
    return $data['app_advertisement_title'] ?? '';
}

function get_app_advertisement_body($data) {
    return $data['app_advertisement_body'] ?? '';
}

function get_app_advertisement_url($data) {
    return $data['app_advertisement_url'] ?? '';
}

function get_icon($data) {
    return $data['icon'] ?? '';
}

function get_keywords($data) {
    return $data['keywords'] ?? '';
}

function get_description($data) {
    return $data['description'] ?? '';
}
?>